#include "m0wig.h"
#include "ui_m0wig.h"
#include "globalval.h"
#include <QTimer>
#include <qmqtt_client.h>
#include "qmqtt_message.h"
#include "homepage.h"
#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"

M0Wig::M0Wig(QWidget *parent) : QWidget(parent),
    ui(new Ui::M0Wig)
{
    ui->setupUi(this);
    group = new QButtonGroup(this);
    group->addButton(ui->zhao, 1);
    group->addButton(ui->feng, 2);
    group->addButton(ui->jing, 3);
    group->addButton(ui->men, 4);
    connect(group, SIGNAL(buttonClicked(int)), this, SLOT(toggledSlot(int)));
}

M0Wig::~M0Wig()
{
    delete ui;
}

void M0Wig::paintEvent(QPaintEvent *event)
{
    // 创建画家类对象
    // 参数为 QPaintDevice *表示可绘制的对象
    QPainter painter(this);

    QPixmap map(":/new/prefix1/beijing.png");
    // 绘制图片
    painter.drawPixmap(0, 0, this->width(), this->height(), map);
}

void M0Wig::toggledSlot(int a)
{QJsonObject json;
  if(a==1)
   { if(zhao==true)
          zhao=false;
      if(zhao==false)
          zhao=true;
       json.insert("key", 1);
       json.insert("val", zhao);

   }
  if(a==2)
  {
      if(feng==true)
                feng=false;
            if(feng==false)
                feng=true;
            json.insert("key", 2);
             json.insert("val", feng);

  }
  if(a==3)
  {if(jing==true)
          jing=false;
       if(jing==false)
           jing=true;
       json.insert("key", 3);
        json.insert("val", jing);
  }
  if(a==4)
  {if(men==true)
          men=false;
       if(men==false)
           men=true;
       json.insert("key", 4);
        json.insert("val", men);
  }
  QJsonDocument doc(json);
  QByteArray dog = doc.toJson();
message1.setPayload(dog);
message1.setTopic("MQTT Examples2");
  qDebug() << "aaaaaaaaaaaaaaaaaaaaaaaaa ";
GlobalVal::mqtt->publish(message1);
}
void M0Wig::dataUpSlot(QByteArray message)
{    ui->mod200->clear();
     ui->mod201->clear();
      ui->mod202->clear();
       ui->mod203->clear();
        ui->mod204->clear();
         ui->mod205->clear();
 ui->mod206->clear();
    QJsonParseError jsonError;
    QJsonDocument ob = QJsonDocument::fromJson(message, &jsonError);
    if (!ob.isNull() && (jsonError.error == QJsonParseError::NoError))
    {
        if (ob.isObject())
        {
            QJsonObject obj = ob.object();
     if (obj.contains("stm32"))
     {
          QJsonValue value  = obj.value("stm32");
            {

                {
                    {
                        if (value.isObject())
                        {
                            QJsonObject subobj = value.toObject();

                            {

                                {
                                    if (subobj.contains("temp"))
                                    {
                                        QJsonValue subValue = subobj.value("temp");

                                      QString ArrayKey = subValue.toString();
                                        qDebug() << "temp : " << ArrayKey;
                                         ui->mod200->append(ArrayKey);
                                    }
                                }

                                {
                                    if (subobj.contains("humi"))
                                    {
                                        QJsonValue subValue = subobj.value("humi");
                                       QString ArrayKey = subValue.toString();
                                        qDebug() << "humi: " << ArrayKey;
                                        ui->mod201->append(ArrayKey);
                                    }
                                }

                                {
                                    if (subobj.contains("Vbat"))
                                    {
                                        QJsonValue subValue = subobj.value("Vbat");
                                        QString ArrayKey = subValue.toString();

                                        qDebug() << "Vbat : " << ArrayKey ;
                                         ui->mod202->append(ArrayKey);

                                    }
                                }

                                {
                                    if (subobj.contains("LD1"))
                                    {
                                        QJsonValue subValue = subobj.value("LD1");
                                       bool ArrayKey = subValue.toBool();
                                       zhao=ArrayKey;
                                      int intValue = static_cast<int>(ArrayKey);  // true 会变成 1，false 会变成 0
                                          QString str = QString::number(ArrayKey);
                                        qDebug() << "LD1 : " << intValue;
                                         ui->mod203->append(str);
                                    }
                                }

                                {
                                    if (subobj.contains("FAN"))
                                    {  subobj.value("FAN")=false;
                                        QJsonValue subValue = subobj.value("FAN");
                                        bool ArrayKey = subValue.toBool();
                                       feng=ArrayKey;

                                       int intValue = static_cast<int>(ArrayKey);  // true 会变成 1，false 会变成 0
                                           QString str = QString::number(ArrayKey);
                                         qDebug() << "FAN : " << intValue;
                                          ui->mod204->append(str);
                                    }
                                }

                                {
                                    if (subobj.contains("FIRE"))
                                    {
                                        QJsonValue subValue = subobj.value("FIRE");
                                       bool ArrayKey = subValue.toBool();
                                       jing=ArrayKey;
                                      int intValue = static_cast<int>(ArrayKey);  // true 会变成 1，false 会变成 0
                                          QString str = QString::number(ArrayKey);
                                        qDebug() << "FIRE : " << intValue;
                                         ui->mod205->append(str);
                                    }
                                    if (subobj.contains("Gate"))
                                    {
                                        QJsonValue subValue = subobj.value("Gate");
                                       bool ArrayKey = subValue.toBool();
                                       jing=ArrayKey;
                                      int intValue = static_cast<int>(ArrayKey);  // true 会变成 1，false 会变成 0
                                          QString str = QString::number(ArrayKey);
                                        qDebug() << "Gate : " << intValue;
                                         ui->mod206->append(str);
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }}
    }
}
